/**
 * @filename:SysRolePermissionServiceImpl 2025-05-27
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.sys.role.service.impl;

import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.generator.genid.IdGenerator;
import com.starhub.engine.sys.role.entity.SysRolePermission;
import com.starhub.engine.sys.role.dao.SysRolePermissionDao;
import com.starhub.engine.sys.role.request.SysRolePermissionBsRequest;
import com.starhub.engine.sys.role.service.SysRolePermissionService;
import com.starhub.utils.Util;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(用户角色关联表设置服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class SysRolePermissionServiceImpl implements SysRolePermissionService {

    @Autowired
    private SysRolePermissionDao rolePermissionDao;

    /**
    * 新增
    * @param user
    * @param request
    * @return
    */
    public List<String> add(LoginUser user, SysRolePermissionBsRequest request){

        List<String> addRoles = new ArrayList<>();
        List<String> roles = request.getRoles();
        List<String> permissions = request.getPermissions();
        if(CollectionUtils.isNotEmpty(roles) && CollectionUtils.isNotEmpty(permissions)){
            SysRolePermission record = new SysRolePermission();
            List<SysRolePermission> rolePermissionList = rolePermissionDao.selectByPrimaryKeySelective(record);
            List<String> existRoles = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(rolePermissionList)) {
                for(SysRolePermission permission:rolePermissionList){
                    String roleId = Util.null2String(permission.getRoleId());
                    String permissionId = Util.null2String(permission.getPermissionId());
                    existRoles.add(roleId+"_"+permissionId);
                }
            }
            for(String role :roles){
                for(String permission :permissions){
                   String id = role+"_"+permission;
                   if(!existRoles.contains(id)){
                       addRoles.add(id+"_"+"0");
                       Long uuid = IdGenerator.generateId();
                       Long roleId = Util.getLongValue(role);
                       Long permissionId = Util.getLongValue(permission);
                       SysRolePermission rolePermission = new SysRolePermission(uuid,roleId,permissionId);
                       rolePermissionDao.insertSelective(rolePermission);
                   }else{
                       addRoles.add(id+"_"+"1");
                   }
                }
            }
        }
        return addRoles;
    }

    /**
    * 更新
    * @param user
    * @param rolePermission
    * @return
    */
    public SysRolePermission update(LoginUser user, SysRolePermission rolePermission){
        //不允许更新的属性设置为null
        rolePermissionDao.updateByPrimaryKeySelective(rolePermission);
        return rolePermission;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public SysRolePermission getInfo(LoginUser user, Long id){

        SysRolePermission rolePermission = rolePermissionDao.selectByPrimaryKey(id);
        return rolePermission;
    }

    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
            //删除状态，目前只有1的逻辑
        rolePermissionDao.deleteSysRolePermissionByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        rolePermissionDao.deleteSysRolePermissionRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<SysRolePermission> getList(LoginUser user,  Map<String , Object>  params){

        List<SysRolePermission> rolePermissionList =  new ArrayList<SysRolePermission>();
        SysRolePermission record = new SysRolePermission();
        rolePermissionList = rolePermissionDao.selectByPrimaryKeySelective(record);
        return rolePermissionList;
    }


}